package com.mars.admin.controller;

import com.mars.admin.controller.base.BaseController;
import com.mars.admin.entity.SysPost;
import com.mars.admin.entity.SysQuest;
import com.mars.admin.framework.common.Result;
import com.mars.admin.service.ISysPostService;
import com.mars.admin.service.ISysQuestService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 面试问题Controller
 * 继承BaseController获得基础的增删改查功能
 *
 * @author tockm
 */
@RestController
@RequestMapping("/system/quest")
@Tag(name = "面试问题管理", description = "面试问题管理相关接口")
public class SysQuestController extends BaseController<SysQuest, Long> {

    @Autowired
    private ISysQuestService sysQuestService;

    /**
     * 分页条件查询面试问题
     *
     * @param pageNum  当前页码
     * @param pageSize 每页大小
     * @param questName 问题分类
     * @param questCode 问题编号
     * @param questDifficulty 问题难度
     * @param status   状态
     * @return 分页结果
     */
    @GetMapping("/pageList")
    @Operation(summary = "分页条件查询岗位", description = "根据条件分页查询岗位列表")
    public Result<Page<SysQuest>> pageList(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "问题分类") @RequestParam(required = false) String questName,
            @Parameter(description = "问题编号") @RequestParam(required = false) String questCode,
            @Parameter(description = "问题难度") @RequestParam(required = false) String questDifficulty,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {

        // 创建查询条件
        SysQuest quest = new SysQuest();
        quest.setQuestName(questName);
        quest.setQuestCode(questCode);
        quest.setQuestDifficulty(questDifficulty);
        quest.setStatus(status);

        // 创建分页对象
        Page<SysQuest> page = new Page<>(pageNum, pageSize);

        // 调用服务层方法进行查询
        Page<SysQuest> questPage = sysQuestService.selectQuestPage(page, quest);

        return Result.success(questPage);
    }


    /**
     * 修改问题状态
     */
    @PutMapping("/changeStatus")
    @Operation(summary = "修改问题状态", description = "修改问题状态")
    public Result<Void> changeStatus(@RequestBody SysQuest quest) {
        sysQuestService.updateById(quest);
        return Result.success();
    }

    /**
     * 批量删除岗位（重写BaseController的方法）
     *
     * @param ids ID列表
     * @return 删除结果
     */
    @Operation(summary = "批量删除问题", description = "根据ID列表批量删除问题及关联数据")
    @DeleteMapping("/batch")
    public Result<Boolean> deleteBatch(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.error("ID列表不能为空");
        }
        Long[] questIds = ids.toArray(new Long[0]);
        boolean success = sysQuestService.deleteQuests(questIds);
        return Result.of(success, "批量删除成功", "批量删除失败");
    }

    /**
     * 根据ID删除岗位（重写BaseController的方法）
     *
     * @param id 主键ID
     * @return 是否成功
     */
    @Operation(summary = "根据ID删除问题", description = "根据主键ID删除问题及关联数据")
    @DeleteMapping("/{id}")
    @Override
    public Result<Boolean> deleteById(@PathVariable Long id) {
        boolean success = sysQuestService.deleteQuestById(id);
        return Result.of(success, "删除成功", "删除失败");
    }
}
